package pub.tbc.atps.util.okhttp;

import com.google.common.base.Charsets;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.nio.charset.Charset;

/**
 * @author tbc on 2016/12/2 12:33:16.
 */
public class ContentType {
    // constants
    public static final ContentType APPLICATION_JSON = create(
            "application/json", Charsets.UTF_8);
    public static final ContentType APPLICATION_FORM_URLENCODED = create(
            "application/x-www-form-urlencoded", Charsets.UTF_8);
    public static final ContentType APPLICATION_XML = create(
            "application/xml", Charsets.UTF_8);
    public static final ContentType TEXT_PLAIN = create(
            "text/plain", Charsets.UTF_8);

    public static final ContentType APPLICATION_ATOM_XML = create(
            "application/atom+xml", Charsets.ISO_8859_1);
    public static final ContentType APPLICATION_OCTET_STREAM = create(
            "application/octet-stream", (Charset) null);
    public static final ContentType APPLICATION_SVG_XML = create(
            "application/svg+xml", Charsets.ISO_8859_1);
    public static final ContentType APPLICATION_XHTML_XML = create(
            "application/xhtml+xml", Charsets.ISO_8859_1);
    public static final ContentType MULTIPART_FORM_DATA = create(
            "multipart/form-data", Charsets.ISO_8859_1);
    public static final ContentType TEXT_HTML = create(
            "text/html", Charsets.ISO_8859_1);

    public static final ContentType TEXT_XML = create(
            "text/xml", Charsets.ISO_8859_1);
    public static final ContentType WILDCARD = create(
            "*/*", (Charset) null);

    // defaults
    public static final ContentType DEFAULT_TEXT = TEXT_PLAIN;
    public static final ContentType DEFAULT_BINARY = APPLICATION_OCTET_STREAM;

    private String mimeType;
    private Charset charset;

    private ContentType(final String mimeType, final Charset charset) {
        this.mimeType = mimeType;
        this.charset = charset;
    }

    private static ContentType create(final String mimeType, final Charset charset) {
        return new ContentType(mimeType, charset);
    }

    public static ContentType create(String mimeType, String charset) {
        return create(mimeType, Charset.forName(charset));
    }

    public static ContentType create(String mimeType) {
        return create(mimeType, Charsets.UTF_8);
    }

}
